﻿namespace MDXStudio
{
    using MDXParser;
    using System;
    using System.Text;

    internal class DebugWatchItem
    {
        internal string m_CMName;
        internal MDXExpNode m_Exp;
        internal string m_ParentCMName;
        internal bool m_watchObject;

        internal void BuildMDX(StringBuilder sb)
        {
            sb.AppendFormat(" MEMBER {0} AS ", this.UniqueName);
            switch (this.m_Exp.GetMDXType())
            {
                case MDXDataType.Unknown:
                case MDXDataType.Set:
                    throw new Exception("Internal error: unexpected type");

                case MDXDataType.Number:
                case MDXDataType.String:
                case MDXDataType.Tuple:
                    if (!this.m_watchObject)
                    {
                        sb.Append(this.m_Exp.GetDebugMDX());
                        return;
                    }
                    sb.AppendFormat("({0}).Item(0).Name", this.m_Exp.GetDebugMDX());
                    return;

                case MDXDataType.Member:
                    if (!this.m_watchObject)
                    {
                        sb.Append(this.m_Exp.GetDebugMDX());
                        return;
                    }
                    sb.AppendFormat("({0}).Name", this.m_Exp.GetDebugMDX());
                    return;

                case MDXDataType.Level:
                case MDXDataType.Hierarchy:
                case MDXDataType.Dimension:
                    sb.AppendFormat("{0}.Name", this.m_Exp.GetDebugMDX());
                    return;

                case MDXDataType.Missing:
                    sb.Append("'<missing>'");
                    return;

                case MDXDataType.Flag:
                    sb.AppendFormat("'{0}'", this.m_Exp.GetLabel());
                    return;
            }
        }

        internal string UniqueName
        {
            get
            {
                return string.Format("{0}.[{1}]", this.m_ParentCMName, this.m_CMName);
            }
        }
    }
}

